package cn.cool.middleware.biz_exception_retry.domain.service;

import cn.cool.middleware.biz_exception_retry.domain.IBizExceptionRetryRecordService;
import cn.cool.middleware.biz_exception_retry.domain.adapter.repository.IBizExceptionRetryRepository;
import cn.cool.middleware.biz_exception_retry.domain.model.entity.BizExceptionRetryRecordEntity;
import cn.cool.middleware.biz_exception_retry.domain.model.entity.RetryRecordEntity;
import cn.cool.middleware.biz_exception_retry.domain.model.valobj.BizExceptionRetryRecordVO;
import cn.cool.middleware.biz_exception_retry.infra.adapter.repository.BizExceptionRetryRepository;
import cn.cool.middleware.biz_exception_retry.types.common.helper.BizMemoryRetryHelper;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
public class BizExceptionRetryRecordService implements IBizExceptionRetryRecordService {
    @Resource
    private IBizExceptionRetryRepository repository;

    /**
     * 插入异常重试表
     * @param entity 实体
     */
    @Override
    public void saveBizExceptionRetryRecord(BizExceptionRetryRecordEntity entity) {
        try {
            BizMemoryRetryHelper.retry(() -> repository.saveExceptionRetry(entity));
        } catch (Throwable throwable) {
            log.error("[@BizExceptionRetry] save exception retry record failed, record = {}", entity, throwable);
        }
    }

    @Override
    public List<BizExceptionRetryRecordVO> listExceptionRetryRecords(Long lastExceptionId, String exceptionModule, Integer pageSize) {
        RetryRecordEntity recordEntity = RetryRecordEntity.builder().lastExceptionId(lastExceptionId).exceptionModule(exceptionModule).pageSize(pageSize).build();
        return repository.listExceptionRetryRecords(recordEntity);
    }

    @Override
    public int updateRecordStatus2Processing(Long bizExceptionRetryId, Integer status) {
        return repository.updateRecordStatus2Processing(bizExceptionRetryId, status);
    }

    @Override
    public int incrBizExceptionRetryDealNumber(Long retryId) {
        return repository.incrBizExceptionRetryDealNumber(retryId);
    }

    @Override
    public int updateBizExceptionRetryRecordStatus(Long retryId, int recordRetryStatus) {
        return repository.updateBizExceptionRetryRecordStatus(retryId, recordRetryStatus);
    }


}

































